<?php


namespace App\Http\Controllers\Common;


use App\Http\Controllers\Controller;
use Exception;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class IndexController extends Controller
{
    /**
     * @return \Illuminate\Http\JsonResponse
     * @throws Exception
     * @author luffyzhao@vip.126.com
     */
    public function upload(){
        return $this->response(
            $this->_file()
        );
    }
    /**
     * file
     * @return array
     * @throws Exception
     * @author luffyzhao@vip.126.com
     */
    protected function _file()
    {
        $uploadedFile = request()->file('file');
        $path = '/' . date('Y/m/d');
        $filename = date('His') . '_' . Str::random(8) . '.' . $uploadedFile->clientExtension();
        if ($file = $uploadedFile->storePubliclyAs($path, $filename)) {
            return [
                'url' => Storage::url($file)
            ];
        } else {
            throw new Exception('文件上传失败');
        }
    }
}
